/*******************************************************************************
																			
	DESCRIPTION: 	This code processes the results from the cyclicality 
					regressions and saves them into Stata format.

*******************************************************************************/

clear all
global id_code 114_3
set seed 2110


/***********************************************************************
* B1: Import data from R
************************************************************************/

frame create betas
frame betas: import delimited "${data}/114_RPanelRegression_Full_2006_relative_trend_rel.csv", clear

frame change default

* Import data:
use "${data}/114_RPanelRegression_Full_2006_relative_trend.dta", clear
	
keep if year == 2006 
keep LopNr_PersonNr InLnr IndivYear key p_emplAft6M_0M_In shareUnempSS
rename IndivYear year
frlink 1:1 key, frame(betas)
frget beta_0 beta_0_se beta_1 beta_1_se beta_t beta_t_se, from(betas)

* Rename variables:
rename beta_1 beta_u
rename beta_1_se beta_u_se

* Generate exponents of intercept from log-log regressions to make it easier to interpret
gen exp_beta_0 = exp(beta_0)


* Shrink the coefficients (see appendix B3 in paper):
foreach x in beta_0 beta_u beta_t {
	
	* Mean
	egen `x'_mean = mean(`x')
	
	* Variance
	egen `x'_sd = sd(`x')
	gen `x'_var = (`x'_sd)^2
	
	* Mean standard error
	egen `x'_se_mean = mean(`x'_se)			
	
	* Shrunken variance (ind. level)
	gen `x'_svar_ind = max(`x'_var - (`x'_se)^2, 0)
	
	* Shrunken variable (ind. level)
	gen `x'_shrunk_ind = `x'_mean + ((`x'_svar_ind/`x'_var)^0.5)*(`x' - `x'_mean)
}

* Save:
save "${data}/${id_code}_CyclicalityBetas_Full_2006.dta", replace

